(function($){
    $.fn.extend({
        loadSelect:function(opt){
            var defaultOpt={
                url:"",
                pId:"",
                brotherUrl:"",
                selectIds:new Array(),
                editFlag:true
            };
            var opts = $.extend(defaultOpt,opt);
            var $this=$(this);
            var editStr = "";
    		if (!opts.editFlag)
    			editStr = "disabled='disabled'";
            $.getJSON(opts.url,{pId:opts.pId},function(json) {
            	if (json != null && json.length > 0) {
            		var html='<select ' + editStr + ' id="cascade_select' + opts.pId + '" onchange="javascript:$(this).selectSelect(\'' + opts.url + '\',' + opts.editFlag + ')"><option value="-1">请选择</option>';
                    $.each(json,function(i,n){
                        html+= '<option value="'+n.id+'">'+n.name+"</option>";
                    }); 
                    html += "</select>";
                    $this.append(html);
                    $("#cascade_select0").find("option[value='" + opts.selectIds[0] + "']").attr("selected", true);
                    
                    if (opts.selectIds && opts.selectIds[1]) {
                    	var selectId1 = opts.selectIds[1];
                		$.getJSON(opts.brotherUrl,{pId:selectId1},function(json) {                    			
                        	if (json != null && json.length > 0) {
                        		var html='<select ' + editStr + ' id="cascade_select' + selectId1 + '" onchange="javascript:$(this).selectSelect(\'' + opts.url + '\',' + opts.editFlag + ')"><option value="-1">请选择</option>';
                                $.each(json,function(i,n){
                                    html+= '<option value="'+n.id+'">'+n.name+"</option>";
                                }); 
                                html += "</select>";
                                $this.append(html);
                                $("#cascade_select" + selectId1).find("option[value='" + selectId1 + "']").attr("selected", true);
                                
                                if (opts.selectIds && opts.selectIds[2]) {
                                	var selectId2 = opts.selectIds[2];
                            		$.getJSON(opts.brotherUrl,{pId:selectId2},function(json) {                    			
                                    	if (json != null && json.length > 0) {
                                    		var html='<select ' + editStr + ' id="cascade_select' + selectId2 + '" onchange="javascript:$(this).selectSelect(\'' + opts.url + '\',' + opts.editFlag + ')"><option value="-1">请选择</option>';
                                            $.each(json,function(i,n){
                                                html+= '<option value="'+n.id+'">'+n.name+"</option>";
                                            }); 
                                            html += "</select>";
                                            $this.append(html);
                                            $("#cascade_select" + selectId2).find("option[value='" + selectId2 + "']").attr("selected", true);
                                    	}
                                    });
                                }
                        	}
                        });
                    }            
                    
            	}
            });
            
        },
        selectSelect:function(url, editFlag){
            var defaultOpt={
                url:url,
                pId:this.val(),
                editFlag:editFlag
            };
            var opts = defaultOpt;
            var $this=$(this);
            var editStr = "";
    		if (!opts.editFlag)
    			editStr = "disabled='disabled'";
            $this.nextAll().remove();
            $.getJSON(opts.url,{pId:opts.pId},function(json){
            	if (json != null && json.length > 0) {
            		var html='<select ' + editStr + ' id="cascade_select' + opts.pId + '" onchange="javascript:$(this).selectSelect(\'' + opts.url + '\',' + opts.editFlag + ')"><option value="-1">请选择</option>';
                    $.each(json,function(i,n){
                        html+= '<option value="'+n.id+'">'+n.name+"</option>";
                    }); 
                    html += "</select>";
                    $this.after(html);
            	}
            });
        }
    });
})(jQuery);

/**
 * 获取级联下拉框的最后一个值
 * @returns
 */
function getLastSelectValue() {
	var selectObj = $("select[id^='cascade_select']");
	var result = selectObj.last().val();
	if (result == -1 && selectObj.last().prev("select[id^='cascade_select']").val() != undefined) {
		result = selectObj.last().prev("select[id^='cascade_select']").val();
	}
	return result;
}

/**
 * 获取级联下拉框的所有值
 * @returns {Array}
 */
function getAllSelectValue() {
	var selectObj = $("select[id^='cascade_select']");
	var result = new Array();
	$(selectObj).each(function(i,item){
		result[i] = $(item).val();
	});
	if (result.length > 1 && result[result.length - 1] == "-1") {
		result.pop();
	}
	return result;
}